Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add From and Into support for SystemTime using references #662

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

neilisaac
Copy link

Enable conversions between Timestamp and SystemTime using references without cloning, and add support for SystemTime::from(Timestamp) in addition to Into (which is derived from the From implementations)

Refer to the note in https://doc.rust-lang.org/std/convert/trait.Into.html

Enable conversions between Timestamp and SystemTime using references without cloning, and add support for SystemTime::from(Timestamp) in addition to Into (which is derived from the From implementations)

Refer to the note in https://doc.rust-lang.org/std/convert/trait.Into.html
@neilisaac
Copy link
Author

Can add the same for Duration too.

@stepancheg
Copy link
Owner

There was a report that this violates Rust conventions: #690

Also, is it Rust convention to convert from &? Are there examples of something similar in rust stdlib? For example, there's no From<&32> for u64.

@neilisaac
Copy link
Author

neilisaac commented Dec 11, 2023

@stepancheg my issue is mainly an ergonomics problem where we need to clone a Timestamp before converting it:

// intuitively I'd like to do
let t: SystemTime = msg.timestamp_field.into();
// or
let t = SystemTime::from(&msg.timestamp_field);

// however we need to do
let t: SystemTime = msg.timestamp_field.clone().into();
// or
let t = SystemTime::from(msg.timestamp_field.clone());

You don't need From<&u32> because u32 supports Copy.

Can Timestamp just implement Copy?

The clone doesn't have any meaningful overhead since there's no allocation, so it's just ergonomics. It's not always obvious how do convert a Timestamp into SystemTime, and I've seen multiple PRs where folks implemented the conversion manually because their ide couldn't suggest a better way.

Based on these conventions I'd probably say that implementing Timestamp::to_system_time(&self) ("borrowed -> owned (non-Copy types)") would be reasonable for a non-Copy struct, however if Timestamp implemented Copy, we wouldn't have this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants